package com.film.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.domain.User;
import com.film.service.HobbyService;
import com.film.service.UserService;
import com.film.service.impl.HobbyServiceImpl;
import com.film.service.impl.UserServiceImpl;

//用户基本信息表
@WebServlet("/userinfor.do")
public class UserInforController extends HttpServlet {
	private HobbyService hobbyService = new HobbyServiceImpl();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		UserService userService=new UserServiceImpl();
		HttpSession session = request.getSession();
		User user =new User();
		user=(User) session.getAttribute("userLogin");
		String uNickName=request.getParameter("nickName");//获取昵称
		String uSex=request.getParameter("gender");//获取性别	
		String year =request.getParameter("year");//获取年份
		String month =request.getParameter("month");//获取月份
		String day =request.getParameter("day");//获取天
		String uBirthday=(year+"-"+month+"-"+day);//拼凑的天数	
		
		String uLiftState=request.getParameter("marriage");//获取生活状态
		String job =request.getParameter("job");//获取主事业
		String occupation =request.getParameter("occupation");//获取细化事业
		String uIndustry=(job+"-"+occupation);//获取事业
		String[] uHobby=request.getParameterValues("interest[]");//获取兴趣
		List<Integer> hobbyIds = new ArrayList<>();
		if(uHobby!=null) {
			for(String o:uHobby) {
				hobbyIds.add(Integer.parseInt(o));
			}
		}
		
		
		String uSignature=request.getParameter("signature");//获取个性签名
		user.setuNickName(uNickName);
		user.setuSex(uSex);
		user.setuBirthday(uBirthday);
		user.setuIndustry(uIndustry);
		user.setuLiftState(uLiftState);
		user.setuSignature(uSignature);
		int result1 = userService.updateById(user);
		response.setCharacterEncoding("utf-8");
		hobbyService.deleteByiduh(user.getuId());
		int result = hobbyService.insertUserHobby(user.getuId(), hobbyIds);
		
		if(result1==1 && result ==1) {
			session.setAttribute("msg",true);
			request.getRequestDispatcher("userEssential.do?uId="+user.getuId().shortValue()+"").forward(request, response);
		}else {
			session.setAttribute("msg",true);
			request.getRequestDispatcher("userEssential.do?uId="+user.getuId().shortValue()+"").forward(request, response);
		}
		
	}
	
	
	
}
